//
//  MBDMsgDetailViewController.swift
//  MasterBoy
//
//  Created by kyson on 2023/11/8.
//

import Foundation
import UIKit


class MBDMsgDetailViewController : MBDBaseViewController {
    
    var tableView : UITableView!
    
    var viewModel : MBDMsgDetailViewModel!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "服务通知"
        
        viewModel = MBDMsgDetailViewModel()

        self.view.backgroundColor = UIColor.white
        
        tableView = UITableView()

        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.allowsMultipleSelection = false
        tableView.allowsSelectionDuringEditing = false
        tableView.allowsMultipleSelectionDuringEditing = false
        self.view.addSubview(tableView)
        
        NSLayoutConstraint.activate([
        self.view.safeAreaLayoutGuide.topAnchor.constraint(equalTo: tableView.topAnchor),
                    self.view.bottomAnchor.constraint(equalTo: tableView.bottomAnchor),
                    self.view.leadingAnchor.constraint(equalTo: tableView.leadingAnchor),
                    self.view.trailingAnchor.constraint(equalTo: tableView.trailingAnchor),
                ])
        tableView.backgroundColor = UIColor(colorHex: 0xF4F4F4)
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        tableView.dataSource = self
        tableView.delegate = self
        tableView.rowHeight = 100
        
        tableView.register(MBDMsgDetailCell.self, forCellReuseIdentifier: "MBDMsgDetailCell")

    }
    
}


extension MBDMsgDetailViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let count = self.viewModel.cellModels?.count ?? 0
        return count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellModel = self.viewModel.cellModels![indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "MBDMsgDetailCell", for: indexPath)
        (cell as! MBDMsgDetailCell ).cellModel = cellModel
        
        return cell
    }
    
    
    func refreshUI(){
//        self.pageIndex = 1
//        viewModel.doRequest { error,result,totalPage in
//
//            if nil != error {
//                self.view.makeToast(error?.localizedDescription,position: .center)
//            } else {
//                self.viewModel.cellModels = result
//                self.tableView.reloadData()
//                self.tableView.mj_header?.endRefreshing()
//
//                if(0 == result?.count) {
//                    self.tableView.setEmptyView(title: "您尚未发布任何群组",
//                                                       message: "赶紧去发布吧")
//                } else {
//                    self.tableView.restore()
//                }
//
//                if totalPage == 1 {
//                    self.tableView.mj_footer?.endRefreshingWithNoMoreData()
//                } else {
//                    self.tableView.mj_footer?.resetNoMoreData()
//                }
//            }
//        }
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let cellModel = self.viewModel.cellModels![indexPath.row]
        return (cellModel as! MBDMsgDetailCellModel).cellHeight
    }
    
}


extension MBDMsgDetailViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cellModel = self.viewModel.cellModels![indexPath.row]
//        let viewModel = MBDUploadGroupViewModel(groupDetail: cellModel.groupDetail,isUpdate: true)
//        let modifyVC = MBDUploadGroupViewController(viewModel: viewModel)
//        self.navigationController?.pushViewController(modifyVC, animated: true)
    }
    
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .delete
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
}
